package com.spring.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.spring.gowhere.R;
import com.spring.httputils.RequestParams;
import com.spring.httputils.RestClient;
import com.spring.httputils.TextHttpResponseHandler;
import com.spring.model.ReturnBagsInfo;
import com.spring.tool.App;
import com.spring.util.Constant;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的钱包
 * 
 * @author Administrator
 * 
 */
public class  MyBagsInfoActivity extends BaseActivity {
	RestClient restClient = null;
	private ListView mybaglistview;
	List<String> listinfo;
	ReturnBagsInfo bagsinfo= null;
	private String username,logintype;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mybagsinfo);
		mybaglistview = (ListView) findViewById(R.id.mybagslistview);
		SharedPreferences userInfo = getSharedPreferences(
				"user_info", 0);
		username = userInfo.getString("username", "");
		logintype = userInfo.getString("logintype", "");
		ImageButton imageButton = (ImageButton) findViewById(R.id.title_left_ib);
		imageButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		TextView textView = (TextView) findViewById(R.id.title_tv);
		textView.setText("我的钱包");
		boolean islianwang =new Constant().isConnect(MyBagsInfoActivity.this);
		if(islianwang){
			restClient = new RestClient(this);
			SendHttpData sendhttpData = new SendHttpData();
			sendhttpData.run();
		}




	}

	class SendHttpData implements Runnable {

		@Override
		public void run() {
			// String url =
			// "http://120.237.110.50:8097/andriodapp/WebService.asmx?op=EmployeeLogin";
			String url = App.getInstance().url+"/QueryBagsInfo";
			RequestParams requestparams = new RequestParams(url, "POST");
			requestparams.put("LoginAccount", username);
			requestparams.put("LoginType", logintype);
			TextHttpResponseHandler textHttpResponseHandler = new TextHttpResponseHandler() {

				@Override
				public void onSuccess(int statusCode, Header[] headers,
						String responseString) {
					// TODO Auto-generated method stub
					Log.e("TAG", "responseString---" + responseString);
					try {
						if(responseString!=null&&!"".equals(responseString)){
							
							AnalysisData(responseString);
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

				@Override
				public void onFailure(int statusCode, Header[] headers,
						String responseString, Throwable throwable) {
					// TODO Auto-generated method stub
					Log.e("TAG", "responseString---" + responseString);

				}
			};
			try {
				restClient.post(url, requestparams, textHttpResponseHandler);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		/**
		 * json数据解析
		 * 
		 * @param strResult
		 * @throws JSONException
		 */
		private void AnalysisData(String strResult) throws JSONException {
			/**
			 * 这里需要分析服务器回传的json格式数据，
			 */
			JSONObject jo = new JSONObject(strResult);
			JSONArray jsonArray = (JSONArray) jo.get("BagsMoney");
			bagsinfo= new ReturnBagsInfo();
			listinfo = new ArrayList<String>();
			for (int i = 0; i < jsonArray.length(); ++i) {
				if(jsonArray.get(i)!=null&&!jsonArray.get(i).equals("0")){
					listinfo.add((String) jsonArray.get(i));
				}
			}
			bagsinfo.setBagsMoney(listinfo);
			JSONArray jsonArrays = (JSONArray) jo.get("BagsName");
			List<String> listinfo2 = new ArrayList<String>();
			for (int i = 0; i < jsonArrays.length(); ++i) {
				listinfo2.add((String) jsonArrays.get(i));
			}
			bagsinfo.setBagsName((listinfo2));
			bagsinfo.setConsumeMoney(jo.getDouble("ConsumeMoney"));
			bagsinfo.setConsumeDate(jo.getString("ConsumeDate"));

			bagsinfo.setReservedField1(jo.getString("ReservedField1"));
			bagsinfo.setReservedField2(jo.getString("ReservedField2"));

			bagsinfo.setReturnMsg(jo.getString("returnMsg"));
			if (listinfo != null&&listinfo.size()>0) {
				MessageAdapter adapter = new MessageAdapter(MyBagsInfoActivity.this,listinfo,bagsinfo);
				mybaglistview.setAdapter(adapter);
			}else{
				Toast.makeText(MyBagsInfoActivity.this, "没有查询到相关数据",  Toast.LENGTH_SHORT).show();
			}
		}
	}
	
	public class MessageAdapter extends BaseAdapter {

		private LayoutInflater mInflater;
		private List<String> returnInfoList;
		ReturnBagsInfo bagsinfo;
		public MessageAdapter(Context context,List<String> lists,ReturnBagsInfo bagsinfo) {
			this.mInflater = LayoutInflater.from(context);
			this.returnInfoList = lists;
			this.bagsinfo = bagsinfo;

		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			if(returnInfoList!=null){				
				return returnInfoList.size();
			}
			return 0;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater
						.inflate(R.layout.mybgasinfoadapter, null);
				holder.txConsumeMoney = (TextView) convertView
						.findViewById(R.id.txConsumeMoney);
				holder.txorder =(TextView)convertView.findViewById(R.id.txorder);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			Log.e("SDFS", "xxq---"+bagsinfo.getBagsMoney().get(position).toString());
			holder.txorder.setText(bagsinfo.getBagsName().get(position).toString());
			holder.txConsumeMoney.setText(bagsinfo.getBagsMoney().get(position).toString());
	
			return convertView;
		}

		public class ViewHolder {
			public TextView txConsumeMoney;
			public TextView txorder;
			

		}

	}
}
